//
//  productCell.m
//  代理端
//
//  Created by mac on 16/3/4.
//  Copyright © 2016年 sliver. All rights reserved.
//

#import "productCell.h"
#import "OrderModel.h"

@interface productCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *productNameLable;
@property (weak, nonatomic) IBOutlet UILabel *disCripeLable;
@property (weak, nonatomic) IBOutlet UILabel *priceLable;
@property (weak, nonatomic) IBOutlet UILabel *countLable;
@property (weak, nonatomic) IBOutlet UILabel *numLable;
@property (weak, nonatomic) IBOutlet UIButton *oneButten;
@property (weak, nonatomic) IBOutlet UIButton *twoButten;
@property(nonatomic,strong)OrderModel *model;
@property (weak, nonatomic) IBOutlet UILabel *reasonLable;

@end
@implementation productCell

+(UITableViewCell *)creatCellFromxib:(NSString *)nib  WithIdentifier:(NSString*)indentfier withtableview:(UITableView *)tableview
{
    UINib *nib1=[UINib nibWithNibName:nib bundle:nil];
    [tableview registerNib:nib1 forCellReuseIdentifier:indentfier];
    return [tableview dequeueReusableCellWithIdentifier:indentfier];
}
- (void)awakeFromNib {
    [self change:self.oneButten];
    [self change:self.twoButten];
    self.iconImageView.layer.masksToBounds = YES;
    self.iconImageView.layer.cornerRadius = 5*KScr;
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    [AutoLayoutUtil autoLayoutWith:self.contentView.subviews];
}
-(void)change:(UIButton *)butten
{
    butten.layer.masksToBounds = YES;
    
    butten.layer.cornerRadius = 5;

}
-(void)drawRect:(CGRect)rect
{
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, rect.size.height)];
    [path addLineToPoint:CGPointMake(rect.size.width, rect.size.height)];
    [DRINK_LIGHTGRAY_COLOR set];
    
    [path stroke];
    
    UIBezierPath *path2 = [UIBezierPath bezierPath];
    
    [path2 moveToPoint:CGPointMake(10*KScr,rect.size.height-68*KScr )];
    
    path2.lineWidth = 0.5*KScr;
    
    [DRINK_LIGHTGRAY_COLOR set];
    
    [path2 addLineToPoint:CGPointMake(rect.size.width-20*KScr,rect.size.height-68*KScr )];
    [path2 stroke];
}

-(void)gaveMeMode:(OrderModel*)model Withindex:(NSInteger)inter
{
    self.model = model;

        model.tag = inter;
        //    NSString *url = [NSString stringWithFormat:@"%@%@",BASE_URL,model.img_thum_cover];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_URL,model.img_thum_cover]];

        [self.iconImageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"kucunzhanwei.png"]];

        self.productNameLable.text = model.commodity_name;

        NSArray *array = [model.selected_options JSONValue];
        NSMutableString *sting =nil;
        for (NSInteger i = 0; i<array.count; i++) {
            NSDictionary *dic = array[i];
            NSString *name = dic[@"name"];
            NSString *valuee= dic[@"value"];
            if (i==0) {
                sting = [NSMutableString stringWithFormat:@"%@:%@",name,valuee];
            }else{
                [sting appendFormat:@"    %@:%@",name,valuee];
            }
            self.disCripeLable.text = sting;
        }
        self.priceLable.text = [NSString stringWithFormat:@"¥%.2f", [model.univalent floatValue]];

        NSLog(@"%@",self.priceLable.text);
        //
        self.countLable.text = [NSString stringWithFormat:@"共:%@件商品",model.count];
        //
        self.numLable.text = [NSString stringWithFormat:@"合计:%.2f",[model.univalent doubleValue]* [model.count
        doubleValue]];

        NSString *sta = [NSString stringWithFormat:@"%@",model.state];
        NSString *stap = [NSString stringWithFormat:@"%@",model.state_step];

        if ([sta isEqualToString:@"11"]) {
            if ([stap isEqualToString:@"4"]) {
                [self lableHiden:NO  with:@"交易失败"];
                [self settitle:@"查看物流" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];

                [self settitle:@"查看原因" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten ];
            }else
            {
                [self lableHiden:YES with:nil];
                [self settitle:@"查看物流" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];
                [self settitle:@"待发货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten ];
            }
        }else if ([sta isEqualToString:@"13"]) {
            [self lableHiden:YES with:nil];
            [self settitle:@"确认收货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];
            [self settitle:@"查看物流" Withcolor:rgb(235, 112, 52, 1) WithTwoHiden:NO Withbtn:self.twoButten];

        }else if ([sta isEqualToString:@"14"])
        {
            [self lableHiden:YES with:nil];
            [self settitle:@"申请退货" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];
            [self settitle:@"删除订单" Withcolor:rgb(235, 112, 52, 1) WithTwoHiden:NO Withbtn:self.twoButten];

        }else if ([sta isEqualToString:@"15"])
        {
            switch ([stap intValue]) {
                case 1:
                    [self btnhiden:YES];
                    [self lableHiden:NO with:@"审核中"];
                    break;
                case 2:
                    [self btnhiden:YES];
                    [self lableHiden:NO with:@"等待总部确认收货"];
                    break;
                case 3:
                    [self lableHiden:NO with:@"驳回"];
                    [self settitle:@"查看原因" Withcolor:DRINK_RED_COLOR WithTwoHiden:NO Withbtn:self.oneButten];

                    [self settitle:@"删除订单" Withcolor:DRINK_RED_COLOR WithTwoHiden:YES Withbtn:self.twoButten];

                    break;
                case 4:
                    [self btnhiden:YES];
                    [self lableHiden:NO with:@"退货成功"];

                default:
                    break;
            }
        }
        else if ([sta isEqualToString:@"12"])
        {
            [self btnhiden:YES];
            [self lableHiden:NO with:@"订单已取消"];
        }

    
    
}
-(void)settitle:(NSString *)title Withcolor:(UIColor *)color WithTwoHiden:(BOOL)Hiden Withbtn:(UIButton *)butten
{
    [butten setTitle:title forState:UIControlStateNormal];
    [butten setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    butten.backgroundColor = color;
    butten.hidden = Hiden;
//    butten.enabled = enable;
}
-(void)btnhiden:(BOOL)hiden
{
    self.oneButten.hidden = hiden;
    self.twoButten.hidden = hiden;
}
-(void)lableHiden:(BOOL)hiden with:(NSString *)message
{
    self.reasonLable.hidden = hiden;
    
    self.reasonLable.text = message;
    
    self.reasonLable.textColor = DRINK_RED_COLOR;
}
- (IBAction)oneBtnClic:(UIButton *)sender {
    NSLog(@"%@",sender.titleLabel.text);
    
    if ([sender.titleLabel.text isEqualToString:@"取消订单"]) {
        self.block(self.model);
    }else if ([sender.titleLabel.text isEqualToString:@"确认收货"])
    {
        if ([self.deleget respondsToSelector:@selector(masureGetProduct:)]) {
            [self.deleget masureGetProduct:self.model];
        }
    
    }else if ([sender.titleLabel.text isEqualToString:@"申请退货"])
    {
        if ([self.deleget respondsToSelector:@selector(applyBackProduct:)]) {
            [self.deleget applyBackProduct:self.model];
        }
    
    }else if ([sender.titleLabel.text isEqualToString:@"查看原因"])
    {
        if ([self.deleget respondsToSelector:@selector(cleckReason:)]) {
            [self.deleget cleckReason:self.model ];
        }
    
    }
    
}
- (IBAction)twoBtnClic:(UIButton *)sender {
    
    if ([sender.titleLabel.text isEqualToString:@"查看物流"]) {
        if ([self.deleget respondsToSelector:@selector(loocForWuliu:)]) {
            [self.deleget loocForWuliu:self.model];
        }
    }else if ([sender.titleLabel.text isEqualToString:@"删除订单"])
    {
        if ([self.deleget respondsToSelector:@selector(delegetOrder:)]) {
            [self.deleget delegetOrder:self.model];
        }
    
    }
    
}

@end
